<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- deployment.qdoc -->
  <title>Deploying QML Applications | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Deploying QML Applications</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#deploying-applications-with-qt-creator">Deploying Applications with Qt Creator</a></li>
<li class="level1"><a href="#qml-caching">QML Caching</a></li>
<li class="level1"><a href="#ahead-of-time-compilation">Ahead-of-Time Compilation</a></li>
<li class="level1"><a href="#prototyping-with-qml-scene">Prototyping with QML Scene</a></li>
<li class="level1"><a href="#initializing-the-qml-runtime-in-applications">Initializing the QML Runtime in Applications</a></li>
<li class="level2"><a href="#initializing-with-qquickview">Initializing with QQuickView</a></li>
<li class="level2"><a href="#creating-a-qqmlengine-directly">Creating a QQmlEngine Directly</a></li>
<li class="level1"><a href="#managing-resource-files-with-the-qt-resource-system">Managing Resource Files with the Qt Resource System</a></li>
<li class="level1"><a href="#related-information">Related Information</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Deploying QML Applications</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-deployment.html-description -->
<div class="descr"> <a name="details"></a>
<p>QML documents are loaded and run by the QML runtime. This includes the Declarative UI engine along with the built-in QML types and plugin modules. The QML runtime also provides access to third-party QML types and modules.</p>
<p>Applications that use QML must invoke the QML runtime to run QML documents. You can do this by creating a <a href="../qtquick/qquickview.html">QQuickView</a> or a <a href="../qtqml/qqmlengine.html">QQmlEngine</a>, as described below. In addition, the Declarative UI package includes the qmlscene tool, which loads <code>.qml</code> files. This tool is useful for developing and testing QML code without having to write a C++ application to load the QML runtime.</p>
<a name="deploying-applications-with-qt-creator"></a>
<h2 id="deploying-applications-with-qt-creator">Deploying Applications with Qt Creator</h2>
<p><a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a> deploys and packages QML applications to various platforms. For mobile devices, Qt Creator can directly bundle applications to the respective platform package formats, such as APK.</p>
<p>When you run your applications on the target platform, your application needs to access the location of the QML libraries. If you use <a href="../qmake/qmake-manual.html">qmake</a>, the <code>QT_INSTALL_QML</code> environment variable points to the location of the libraries. The <a href="http://qt.io/download">Qt Installers</a> install the QML libraries in: <code>&lt;version&gt;</code><code>/</code><i>&lt;compiler&gt;</i><code>/qml</code> directory.</p>
<a name="qml-caching"></a>
<h2 id="qml-caching">QML Caching</h2>
<p>The QML runtime loads QML documents by parsing them and generating byte code. Most of the time, the document hasn't changed since the last time it was loaded. To speed up this loading process, the QML runtime maintains a cache file for each QML document. This cache file contains the compiled byte code and a binary representation of the QML document structure. In addition, when multiple applications use the same QML document, the memory needed for the code is shared between application processes. The cache files are loaded via the <code>mmap()</code> system call on POSIX-compliant operating systems or <code>CreateFileMapping()</code> on Windows, resulting in significant memory savings.</p>
<p>Each time you load a changed QML document, the cache is automatically re-created. Cache files are located in a sub-directory of <a href="../qtcore/qstandardpaths.html#StandardLocation-enum">QStandardPaths::CacheLocation</a> with the name &quot;qmlcache&quot;. The file extension is <code>.qmlc</code> for QML documents and <code>.jsc</code> for imported JavaScript modules.</p>
<a name="compiling-ahead-of-time"></a><a name="ahead-of-time-compilation"></a>
<h2 id="ahead-of-time-compilation">Ahead-of-Time Compilation</h2>
<p>The automatic caching of compiled QML documents into cache files results in significantly faster application load time. However, the initial creation of cache files can still take time, especially when the application starts for the very first time. To avoid that initial step and provide faster startup times from the very beginning, Qt's build system allows you to perform the compilation step for QML files ahead of time, when compiling the C++ parts of your application.</p>
<p>To deploy your application with QML files compiled ahead of time, you must organize the files and the build system in a specific way:</p>
<ul>
<li>All QML documents (including JavaScript files) must be included as resources via <a href="../qtcore/resources.html">Qt's Resource system</a>.</li>
<li>Your application must load the QML documents via the <code>qrc:///</code> URL scheme.</li>
<li>You can enable Ahead-of-Time compilation using the <code>CONFIG+=qtquickcompiler</code> directive.</li>
<li>If you're using the CMake build system, then you can achieve this by inserting a <code>find_package(Qt5QuickCompiler)</code> call into your <code>CMakeLists.txt</code> and replacing the use of <code>qt5_add_resources</code> with <code>qtquick_compiler_add_resources</code>.</li>
</ul>
<p>One benefit of compiling ahead of time is that, in the event of syntax errors in your QML documents, you are notified at application compile-time instead of at run-time, when the file is loaded.</p>
<p>If you have <code>.qml</code> or <code>.js</code> files which should not be compiled but just bundled by the resource system, such as <code>.js</code> files used with <a href="../qtwebengine/qtwebengine-index.html">Qt WebEngine</a>, you can omit them from the compilation via the <code>QTQUICK_COMPILER_SKIPPED_RESOURCES</code> variable. In your project file, specify the resource files to omit, as follows:</p>
<pre class="cpp">

  QTQUICK_COMPILER_SKIPPED_RESOURCES <span class="operator">+</span><span class="operator">=</span> bundle_only<span class="operator">.</span>qrc

</pre>
<p>By default, this feature ties your application to the Qt version you are compiling against, because it replaces the QML document source code in the resources with the compiled binary version. The source files are not present anymore. Consequently, when you use the same application against a different version of Qt without recompiling it, loading the QML documents will fail with an error message.</p>
<p>Ahead-of-Time compilation is implemented this way because the feature originates from an add-on for use in commercial application environments, where deploying source code is not desirable but it's usually acceptable to require a recompilation when changing Qt.</p>
<p>You can retain the QML and JavaScript documents in the resources by passing a list of resource <code>(*.qrc)</code> files in the <code>QTQUICK_COMPILER_RETAINED_RESOURCES</code> qmake variable. These resource files will then not be filtered, and any QML and JavaScript files specified in them will be readable in full source by your application. Then, if you run the application with a different Qt version, the QML and JavaScript files will be recompiled at runtime, slowing down the initial start of the application.</p>
<a name="prototyping-with-qml-scene"></a>
<h2 id="prototyping-with-qml-scene">Prototyping with QML Scene</h2>
<p>The Declarative UI package includes a QML runtime tool, <a href="qtquick-qmlscene.html">qmlscene</a>, which loads and displays QML documents. This is useful during the application development phase for prototyping QML-based applications without writing your own C++ applications to invoke the QML runtime.</p>
<a name="initializing-the-qml-runtime-in-applications"></a>
<h2 id="initializing-the-qml-runtime-in-applications">Initializing the QML Runtime in Applications</h2>
<p>To run an application that uses QML, your application must invoke the QML runtime. This is done by writing a Qt C++ application that loads the <a href="../qtqml/qqmlengine.html">QQmlEngine</a> by either:</p>
<ul>
<li>Loading the QML file through a <a href="../qtquick/qquickview.html">QQuickView</a> instance.</li>
<li>Creating a <a href="../qtqml/qqmlengine.html">QQmlEngine</a> instance and loading QML files with <a href="../qtqml/qqmlcomponent.html">QQmlComponent</a>.</li>
</ul>
<a name="initializing-with-qquickview"></a>
<h3 id="initializing-with-qquickview">Initializing with QQuickView</h3>
<p><a href="../qtquick/qquickview.html">QQuickView</a> is a <a href="../qtgui/qwindow.html">QWindow</a>-based class that can load QML files. For example, if there is a QML file, <code>application.qml</code>, it will look like this:</p>
<pre class="qml">

  import QtQuick 2.3

  <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>; <span class="name">color</span>: <span class="string">&quot;red&quot;</span> }

</pre>
<p>It can be loaded in a Qt application's <code>main.cpp</code> file like this:</p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QGuiApplication&gt;</span>
  <span class="preprocessor">#include &lt;QQuickView&gt;</span>

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span> app(argc<span class="operator">,</span> argv);

      <span class="type"><a href="../qtquick/qquickview.html">QQuickView</a></span> view;
      view<span class="operator">.</span>setSource(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">&quot;application.qml&quot;</span>));
      view<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>This creates a <a href="../qtgui/qwindow.html">QWindow</a>-based view that displays the contents of <code>application.qml</code>.</p>
<p>The application's <code>.pro</code> <a href="../qmake/qmake-project-files.html">project file</a> must specify the <code>declarative</code> module for the <code>QT</code> variable. For example:</p>
<pre class="cpp">

  TEMPLATE <span class="operator">+</span><span class="operator">=</span> app
  QT <span class="operator">+</span><span class="operator">=</span> quick
  SOURCES <span class="operator">+</span><span class="operator">=</span> main<span class="operator">.</span>cpp

</pre>
<a name="creating-a-qqmlengine-directly"></a>
<h3 id="creating-a-qqmlengine-directly">Creating a QQmlEngine Directly</h3>
<p>If <code>application.qml</code> doesn't have any graphical components, or if it's preferred to avoid <a href="../qtquick/qquickview.html">QQuickView</a> for other reasons, the <a href="../qtqml/qqmlengine.html">QQmlEngine</a> can be constructed directly instead. In this case, <code>application.qml</code> is loaded as a <a href="../qtqml/qqmlcomponent.html">QQmlComponent</a> instance rather than placed into a view:</p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QGuiApplication&gt;</span>
  <span class="preprocessor">#include &lt;QQmlEngine&gt;</span>
  <span class="preprocessor">#include &lt;QQmlContext&gt;</span>
  <span class="preprocessor">#include &lt;QQmlComponent&gt;</span>

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span> app(argc<span class="operator">,</span> argv);

      <span class="type"><a href="../qtqml/qqmlengine.html">QQmlEngine</a></span> engine;
      <span class="type"><a href="../qtqml/qqmlcontext.html">QQmlContext</a></span> <span class="operator">*</span>objectContext <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtqml/qqmlcontext.html">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());

      <span class="type"><a href="../qtqml/qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&amp;</span>engine<span class="operator">,</span> <span class="string">&quot;application.qml&quot;</span>);
      <span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>object <span class="operator">=</span> component<span class="operator">.</span>create(objectContext);

      <span class="comment">// ... delete object and objectContext when necessary</span>

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>If you're not using any graphical items from Qt Quick, you can replace <a href="../qtgui/qguiapplication.html">QGuiApplication</a> with a <a href="../qtcore/qcoreapplication.html">QCoreApplication</a> in the code above. This way, you can use QML as a language without any dependencies to the <a href="../qtgui/qtgui-index.html">Qt GUI</a> module.</p>
<a name="managing-resource-files-with-the-qt-resource-system"></a>
<h2 id="managing-resource-files-with-the-qt-resource-system">Managing Resource Files with the Qt Resource System</h2>
<p>The <a href="../qtcore/resources.html">Qt resource system</a> allows resource files to be stored as binary files in an application executable. This can be useful when building a mixed QML/C++ application as it enables QML files and other resources -- such as images and sound files -- to be referred to through the resource system URI scheme rather than relative or absolute paths to filesystem resources.</p>
<p><b>Note: </b>If you use the resource system, the application executable must be re-compiled whenever a QML source file is changed, to update the resources in the package.</p><p>To use the resource system in a mixed QML/C++ application:</p>
<ul>
<li>Create a <code>.qrc</code> <a href="../qtcore/resources.html">resource collection file</a> that lists resource files in XML format.</li>
<li>From C++, load the main QML file as a resource using the <code>:/</code> prefix or as a URL with the <code>.qrc</code> scheme.</li>
</ul>
<p>Once this is done, all files specified by relative paths in QML are loaded from the resource system instead. Use of the resource system is completely transparent to the QML layer; this means all QML code should refer to resource files using relative paths and should <b>not</b> use the <code>.qrc</code> scheme. This scheme should only be used from C++ code to refer to resource files.</p>
<p>Here's an application packaged using the Qt resource system; its directory structure is as follows:</p>
<pre class="cpp">

  project
      <span class="operator">|</span><span class="operator">-</span> example<span class="operator">.</span>qrc
      <span class="operator">|</span><span class="operator">-</span> main<span class="operator">.</span>qml
      <span class="operator">|</span><span class="operator">-</span> images
          <span class="operator">|</span><span class="operator">-</span> background<span class="operator">.</span>png
      <span class="operator">|</span><span class="operator">-</span> main<span class="operator">.</span>cpp
      <span class="operator">|</span><span class="operator">-</span> project<span class="operator">.</span>pro

</pre>
<p>The <code>main.qml</code> and <code>background.png</code> files are packaged as resource files. This is done in the <code>example.qrc</code> resource collection file:</p>
<pre class="cpp">

  &lt;!DOCTYPE RCC&gt;
  &lt;RCC version=&quot;1.0&quot;&gt;

  &lt;qresource prefix=&quot;/&quot;&gt;
      &lt;file&gt;main.qml&lt;/file&gt;
      &lt;file&gt;images/background.png&lt;/file&gt;
  &lt;/qresource&gt;

  &lt;/RCC&gt;

</pre>
<p>Since <code>background.png</code> is a resource file, <code>main.qml</code> can refer to it using the relative path specified in <code>example.qrc</code>:</p>
<pre class="qml">

  <span class="comment">// main.qml</span>
  import QtQuick 2.3

  <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> { <span class="name">source</span>: <span class="string">&quot;images/background.png&quot;</span> }

</pre>
<p>To allow QML to locate resource files correctly, the <code>main.cpp</code> loads the main QML file, <code>main.qml</code>, as a resource file using the <code>.qrc</code> scheme:</p>
<pre class="cpp">

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="type"><a href="../qtwidgets/qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

      <span class="type"><a href="../qtquick/qquickview.html">QQuickView</a></span> view;
      view<span class="operator">.</span>setSource(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>(<span class="string">&quot;qrc:/main.qml&quot;</span>));
      view<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>Finally, <code>project.pro</code> uses the <code>RESOURCES</code> variable to indicate that <code>example.qrc</code> should be used to build the application resources:</p>
<pre class="cpp">

  QT += qml

  SOURCES += main.cpp
  RESOURCES += example.qrc

</pre>
<a name="related-information"></a>
<h2 id="related-information">Related Information</h2>
<ul>
<li><a href="deployment.html">Deploying Qt Applications</a></li>
<li><a href="http://doc.qt.io/qtcreator/creator-running-targets.html">Running on Multiple Platforms</a></li>
<li><a href="http://doc.qt.io/qtcreator/creator-deployment.html">Deploying to Devices</a></li>
<li><a href="../qtqml/qtqml-cppintegration-data.html">qtqml-cppintegration-exposecppattributes.html</a>{Exposing Attributes of C++ Types to QML}</li>
<li><a href="../qtcore/resources.html">The Qt Resource System</a></li>
</ul>
</div>
<!-- @@@qtquick-deployment.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
